import { DataXTaskLog } from '@/service/modules/task-manage/types'
import { FormRules, NForm } from 'naive-ui'
import { h } from 'vue'
import { useFailed } from '../fields/use-failed'
import { useLocalParams } from '../fields/use-local-params'

const useDataX = (taskLog: DataXTaskLog) => {
  const rules: FormRules = {
    failRetryTimes: {
      trigger: ['input', 'blur'],
      required: true,
      validator() {
        if (
          taskLog.taskParams.failRetryTimes !== 0 &&
          !taskLog.taskParams.failRetryTimes
        ) {
          return new Error(window.$t('taskManage.failRetryTimesTips'))
        }
      }
    },
    failRetryInterval: {
      trigger: ['input', 'blur'],
      required: true,
      validator() {
        if (
          taskLog.taskParams.failRetryInterval !== 0 &&
          !taskLog.taskParams.failRetryInterval
        ) {
          return new Error(window.$t('taskManage.failRetryIntervalTips'))
        }
      }
    }
  }
  return h('div', null, [
    h(
      'h4',
      { style: 'margin:0' },
      window.$t('taskManage.dataSyncTaskParamsConfig')
    ),
    h(
      NForm,
      {
        rules: rules,
        labelPlacement: 'left',
        requireMarkPlacement: 'right-hanging',
        labelWidth: 'auto',
        showFeedback: false
      },
      [...useFailed(taskLog), useLocalParams(taskLog)]
    )
  ])
}
export default useDataX
